<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
//app1, app2, app3, app4
// app1 = 1, menu flag
// app2 is the menu id, the content is from menu
// app3 = 1, category flag
// app4 is the category id, the content is category page

//type
// 0	content/article
// 1	announcement
// 2	activity
// 3	blog
// 4	event
// 5	...

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class ContentBase extends BaseItemBase {
	//type
	var $_type = 0;
	
	//constructor
	function ContentBase() {
		parent::BaseItemBase();
		
		$this->item_link = true;
		$this->item_link_owner = true;
		$this->table = 'pp_contents';
		$this->prefix = 'contents_';
		
		$this->sort_order = true;
		$this->sort_fields = array('id-');
		
		$this->filter_fields = array(
							'cid:=', 'parent:=', 'app1:=', 'app2:=', 'app2:>', 'app3:=', 'app3:>', 'app4:=', 'app4:>', 
							'flag_recommend:=', 'flag_top:=', 'flag_level:=', 'flag_digest:=',
								);
		$this->filter_map = array(
						'cid' => 'cid:=', 'parent' => 'parent:=', 
						'recommend' => 'flag_recommend:=', 'recommend:=' => 'flag_recommend:=', 'top' => 'flag_top:=', 'top:=' => 'flag_top:=',
							);
	}
	
	//setter/getter
	function setType($type = 0) {
		if( $type > 0 ) {
			$this->_type = $type;
		}
	}
	
	function setTypeDetail($type) {
		$types = array(
				'content' => 0, 
				'announcement' => 1, 
				'activity' => 2, 
				'blog' => 3, 
				'event' => 4, 
					);
		if( $type != '' && isset($types[$type]) ) {
			$this->_type = $types[$type];
		}
	}
	
	//methods
	function onAddItemFilterBind($data_array) {
		$fb = array();
		if( $this->_type > 0 ) {
			$fb['_type'] = (int)$this->_type;
		}
		
		//banner
		if( isset($data_array['contents_image_banner']) && $data_array['contents_image_banner'] != '' ) {
			$fb['_banner'] = $data_array['contents_image_banner'];
			$fb['_banner_rid'] = $data_array['contents_image_param_banner'];
			
			//
			$fb['_images'] = serialize(array(
						'banner' => array(
								'value' => $data_array['contents_image_banner'],
								'width' => $data_array['contents_image_width_banner'],
								'height' => $data_array['contents_image_height_banner'],
								'rid' => $data_array['contents_image_param_banner'],
									),
							));
		}
		
		//fulltext
		if( isset($data_array['contents_fulltext']) ) {
			$separator = '<!-- pagebreak -->';
			$multipage = sizeof(explode($separator, $data_array['contents_fulltext']));
			
			$fb['contents_pagecount'] = $multipage;
		}
		
		return $fb;
	}
	
	function onUpdateItemFilterBind($items_id, $data_array) {
		$fb = array();
		if( $this->_type > 0 ) {
			$fb['_type'] = (int)$this->_type;
		}
		
		//banner
		if( isset($data_array['contents_image_banner']) && $data_array['contents_image_banner'] != '' ) {
			$fb['_banner'] = $data_array['contents_image_banner'];
			$fb['_banner_rid'] = $data_array['contents_image_param_banner'];
			
			//
			$fb['_images'] = serialize(array(
						'banner' => array(
								'value' => $data_array['contents_image_banner'],
								'width' => $data_array['contents_image_width_banner'],
								'height' => $data_array['contents_image_height_banner'],
								'rid' => $data_array['contents_image_param_banner'],
									),
							));
		}
		
		//fulltext
		if( isset($data_array['contents_fulltext']) ) {
			$separator = '<!-- pagebreak -->';
			$multipage = sizeof(explode($separator, $data_array['contents_fulltext']));
			
			$fb['contents_pagecount'] = $multipage;
		}
		
		return $fb;
	}
	
	function onFormatItem($item) {
		if( $item['images'] != '' ) {
			$images = unserialize($item['images']);
			if( isset($images['banner']) && is_array($images['banner']) ) {
				$item['banner'] = $images['banner']['value'];
				$item['banner_width'] = $images['banner']['width'];
				$item['banner_height'] = $images['banner']['height'];
				$item['banner_rid'] = $images['banner']['rid'];
			}
		}
		
		return $item;
	}
	
	//
	function createItemCategory($item) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/content/content.category.class.php');
		$mcObj = new ContentCategoryBase();
		$mcObj->format = true;
		
		//
		$uk = 'content:' . $item['id'];
		$sql = "select count(*) as count from " . $mcObj->table . " where categories_type = '" . $uk . "'";
		$count = $this->adb->getCount($sql);
		if( $count > 0 ) {
			return 2;
		}
		//
		$data_array = array(
					'categories_name' => $item['name'],
					'categories_link' => $item['link'],
					'categories_description' => $item['description'],
					'categories_type' => 'content:' . $item['id'],
					'categories_parent' => $item['cid'],
						);
		$cid = $mcObj->addCategory($data_array);
		
		$data_array = array('_app4' => $cid);
		$this->updateItem($item['id'], $data_array);
		
		return 1;
	}
	
	//private
	function _get_query_str_custom() {
		$query_str = "";
		if( $this->_type > 0 ) {
			$query_str .= " and " . $this->_pf('type') . " = '" . (int)$this->_type . "'";
		}
		
		return $query_str;
	}
}
//
?>